package controller;

import util.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author gaojie  gao4433@qq.com
 * @Description
 * @date 2022/6/10 4:08
 * @since 1.8
 */
public class GoodsUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 取值
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("goodsId");
        int categoryId = Integer.parseInt(request.getParameter("category")) ;
        String goodsName = request.getParameter("goodsName");
        String price = request.getParameter("price");
        String imgUrl = request.getParameter("imgUrl");
        String comments = request.getParameter("comments");

        // 2. 储值
        if (id != null && !"null".equals(id)) {
            // 存在 id，执行更新
            String sql = "UPDATE `ec_simple_goods` SET `cid` = ?, title = ?, price = ?, image = ?, comment = ? WHERE id = ?";
            JdbcUtils.modify(sql, categoryId, goodsName, price, imgUrl, comments, id);
        } else {
            // 不存在 id，执行插入
            String sql = "INSERT INTO `ec_simple_goods` (`cid`,`title`,`price`,`image`,`comment`) VALUES(?,?,?,?,?)";
            JdbcUtils.modify(sql, categoryId, goodsName, price, imgUrl, comments);
        }
        //
    }
}
